﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.WinKStart.Abstract;

namespace WMSSoft.WinKStart.ViewModel
{
    public class CContentContextMenuViewModel : CCommandViewModel
    {
        System.Windows.Controls.ListView _DataListView = null;
        List<CContextMenuTargetItem> _Data = null;
        CContentConfig _ContentConfig = null;
        object _SelectedDataItem = null;
        bool _CanWorkWithItems = false;
        int _NextMenuId = -1;

        public CContentContextMenuViewModel(CContentConfig Config) 
        {
            _ContentConfig = Config;
            _NextMenuId = _ContentConfig.NextMenuID;
            PropertyChanged += CContentContextMenuViewModel_PropertyChanged;
        }

        public CContentContextMenuViewModel(List<CContextMenuTargetItem> Data, CContentConfig Config)
        {
            _ContentConfig = Config;
            PropertyChanged += CContentContextMenuViewModel_PropertyChanged;
            _Data = Data;
            _NextMenuId = _ContentConfig.NextMenuID;
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_AddNewContextMenuItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_EditContextMenuItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_DeleteContextMenuItem())));
        }

        ~CContentContextMenuViewModel()
        {
            PropertyChanged -= CContentContextMenuViewModel_PropertyChanged;
        }

        public CContentContextMenuViewModel(string ViewName)
        {
            PropertyChanged += CContentContextMenuViewModel_PropertyChanged;
            base.ViewName = ViewName;
        }

        #region Properties
        /// <summary>
        /// Contains data to view in Listview
        /// </summary>
        public List<CContextMenuTargetItem> Data
        {
            get { return _Data; }
        }

        /// <summary>
        /// Contains true if the user has selected an item in ListView
        /// </summary>
        public bool CanWorkWithItems
        {
            get { return _CanWorkWithItems; }
            set 
            { 
                _CanWorkWithItems = value;
                OnPropertyChanged("CanWorkWithItems");
            }
        }

        /// <summary>
        /// Contains the in ListView from user selected item
        /// </summary>
        public object SelectedDataItem
        {
            get { return _SelectedDataItem; }
            set 
            { 
                _SelectedDataItem = value;
                OnPropertyChanged("SelectedDataItem");
            }
        }

        public CSingleCommandViewModel Cmd_Add
        {
            get { return m_oCommands[0]; }
        }

        public CSingleCommandViewModel Cmd_Edit
        {
            get { return m_oCommands[1]; }
        }

        public CSingleCommandViewModel Cmd_Delete
        {
            get { return m_oCommands[2]; }
        }

        #endregion

        void SetCanWorkWithItems()
        {
            if (_SelectedDataItem != null)
                CanWorkWithItems = true;
            else
                CanWorkWithItems = false;
           
        }

        #region Command Procedures
        void Cmd_AddNewContextMenuItem()
        {
            try
            {               
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Add.Command).CommandParameter != null)
                {
                    _DataListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Add.Command).CommandParameter;

                    Settings_NewEditContextMenuTargetViewModel tmpViewModel = new Settings_NewEditContextMenuTargetViewModel(_ContentConfig);
                    tmpViewModel.DataChanged += tmpViewModel_DataChanged;
                    Settings_NewEditContextMenuTarget oNewContextDlg = new Settings_NewEditContextMenuTarget();
                    _SelectedDataItem = null;

                    oNewContextDlg.DataContext = tmpViewModel;
                    if (oNewContextDlg.ShowDialog() == true)
                    {
                        Data.Add(((Settings_NewEditContextMenuTargetViewModel)oNewContextDlg.DataContext).Data);
                        if (_DataListView != null) _DataListView.Items.Refresh();

                    }

                    _DataListView = null;
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Can't create new Context-Menu - Item!", Ex, EMessageTypes.Error);
            }
        }

        void tmpViewModel_DataChanged(object sender, EventArgs e)
        {
            try
            {
                if (_SelectedDataItem == null)
                {
                    //Add new Context-Menu-Target-Item
                    if (FindTarget(((Settings_NewEditContextMenuTargetViewModel)sender).Data.Name) == false)
                    {
                        Data.Add(((Settings_NewEditContextMenuTargetViewModel)sender).Data);
                    }
                }
                else
                {
                    //applying to existing item
                    _SelectedDataItem = ((Settings_NewEditContextMenuTargetViewModel)sender).Data;
                   
                }
                if (_DataListView != null) _DataListView.Items.Refresh();
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on applying Context-Menu-Target changes!", Ex, EMessageTypes.Error);
            }
        }

        void Cmd_EditContextMenuItem()
        {
            try
            {                
                 if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Edit.Command).CommandParameter != null)
                 {
                     _DataListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Edit.Command).CommandParameter;
                     if (_SelectedDataItem != null)
                     {
                         Settings_NewEditContextMenuTargetViewModel tmpViewModel = new Settings_NewEditContextMenuTargetViewModel((CContextMenuTargetItem)_SelectedDataItem, _ContentConfig);
                         tmpViewModel.DataChanged += tmpViewModel_DataChanged;
                         Settings_NewEditContextMenuTarget oNewContextDlg = new Settings_NewEditContextMenuTarget();
                         oNewContextDlg.DataContext = tmpViewModel;
                         if (oNewContextDlg.ShowDialog() == true)
                         {
                             _SelectedDataItem = ((Settings_NewEditContextMenuTargetViewModel)oNewContextDlg.DataContext).Data;
                             if (_DataListView != null) _DataListView.Items.Refresh();
                         }

                     }
                     _DataListView = null;
                 }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Can't create new Context-Menu - Item!", Ex, EMessageTypes.Error);
            }
        }

        void Cmd_DeleteContextMenuItem()
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Delete.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_Delete.Command).CommandParameter;
                    if (_SelectedDataItem != null)
                    {
                        string sCaption = ((cWinKStartContextMenuItem)_SelectedDataItem).Caption;
                        if (System.Windows.MessageBox.Show(LanguageStrings.GetText("Delete selected Context-Menu-Target?"), 
                            LanguageStrings.GetText("Question"),
                            System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                        {
                            Data.Remove((CContextMenuTargetItem)_SelectedDataItem);
                            if (tmpListView != null) tmpListView.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Can't delete selected Context-Menu - Item!", Ex, EMessageTypes.Error);
            }
        }

        #endregion

        void CContentContextMenuViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "SelectedDataItem")
                    SetCanWorkWithItems();
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on changed Property '" + e.PropertyName + "'!", Ex, EMessageTypes.Error);
            }
        }

        bool FindTarget(string Name)
        {
            try
            {
                if (_Data != null)
                {
                    var ListResult = (from Target in _Data where Target.Name.ToUpper() == Name.ToUpper() select Target);
                    if (ListResult != null && ListResult.Count() > 0)
                        return true;
                    else
                        return false;
                }
                else return false;
            }
            catch (Exception Ex)
            {
                throw new Exception("Error on find Region", Ex);
            }
        }

    }
}
